package com.bit.cfdi.web.validators.init;

import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.util.Initiator;

import com.bit.cfdi.web.constants.CFDIConstants;
import com.bit.cfdi.web.session.SessionUtils;

public class CheckLoged implements Initiator {
	
	private Logger log = LogManager.getLogger(this.getClass().getName());

	@Override
	public void doInit(Page page, Map<String, Object> args) throws Exception {
		
		SessionUtils sessionUtils = null;		
		Object posibleSessionUtils = args.get(
				CFDIConstants.ServicesKeys.SESSION_UTILS_KEY);
		if(posibleSessionUtils instanceof SessionUtils) {
			sessionUtils = (SessionUtils) posibleSessionUtils;
		}
		if(sessionUtils == null) {
			log.error("FATAL ERROR: SessionUtils not in context");
			return;
		}
		if(sessionUtils.getFromSession(SessionUtils.USUARIO) != null) {
			log.warn("User already logged in the session, " +
					"redirecting to home page");
			Executions.sendRedirect(CFDIConstants.CFDIPages.HOME_URL);
		}
	}

}
